#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

// http://stackoverflow.com/questions/2031109/understanding-set-getsockopt-so-sndbuf
int main(int argc, char * argv[]) {
	int sockfd;
	socklen_t optlen;

	// stdlib.h
	char *user;
	if ( (user = getenv("USER")) != NULL) {
		printf("env $USER=%s\n", user);
	}

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("ERROR socket");
		exit(1);
	}

	// SO_RCVLOWAT
	int rcvlowat = -1;
	optlen = sizeof(rcvlowat);
	if (getsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat, &optlen) < 0) {
		printf("Error getsockopt \n");
	} else printf("SO_RCVLOWAT = %d\n", rcvlowat);


	// SO_SNDLOWAT
	int sndlowat = -1;
	optlen = sizeof(rcvlowat);
	if (getsockopt(sockfd, SOL_SOCKET, SO_SNDLOWAT, &sndlowat, &optlen) < 0) {
		printf("Error getsockopt \n");
	} else printf("SO_SNDLOWAT = %d\n", sndlowat);


	// tcp_maxseg
	int tcpmaxseg = -1;
	optlen = sizeof(tcpmaxseg);
	if (getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &tcpmaxseg, &optlen) < 0) {
		printf("Error getsockopt \n");
	} else printf("TCP_MAXSEG = %d\n", tcpmaxseg);

	// sndbuf
	int sndbuf = -1;
	optlen = sizeof(sndbuf);
	if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0) {
		printf("Error getsockopt \n");
	} else printf("SO_SNDBUF = %d\n", sndbuf);


	// rcvbuf
	int rcvbuf = -1;
	optlen = sizeof(rcvbuf);
	if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) < 0) {
		printf("Error getsockopt \n");
	} else printf("SO_SNDBUF = %d\n", rcvbuf);
	return 0;
}